﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// AgriRSModel_Disease 的摘要说明
/// </summary>
public class AgriRSModel_Disease
{
	 private  DataSet _AgriParamDataset;
     private DataSet _RSIndexDataset;
    
    //public AgriRSModel()
    //{
    //    //
    //    // TODO: 在此处添加构造函数逻辑
    //    //
    //    AgriParamDataset = new DataSet();
    //    RSIndexDataset = new DataSet();
    //}

    public AgriRSModel_Disease(string agriParamName, string rsIndexName, string samplingDate, string districtId)
    {
        string queryString = "";
        if (Convert.ToInt32(districtId) > 50)
            queryString = "select PointID, " + agriParamName + " from rs.sp_DISEASE_PARAM where sampdate = to_date('" + samplingDate + "','yyyy-mm-dd') and DistId = '" + districtId + "' order by PointID";
        else
            queryString = "select PointID, " + agriParamName + " from rs.sp_DISEASE_PARAM  where sampdate = to_date('" + samplingDate + "','yyyy-mm-dd') and ROOTID = '" + districtId + "' order by PointID";

        _AgriParamDataset = OracleDataBase.Query(queryString);
        //return ds;

        //string queryString = "";
        if (Convert.ToInt32(districtId) > 50)
            queryString = "select PointID, " + rsIndexName + " from rs.sp_disease_vi where sampdate = to_date('" + samplingDate + "','yyyy-mm-dd') and DistId = '" + districtId + "' order by PointID";
        else
            queryString = "select PointID, " + rsIndexName + " from rs.sp_disease_vi where sampdate = to_date('" + samplingDate + "','yyyy-mm-dd') and ROOTID = '" + districtId + "' order by PointID";

        _RSIndexDataset = OracleDataBase.Query(queryString);
        //return ds;

    }

  

    //~AgriRSModel()::IDisposable
    //{
    //    _AgriParamDataset.Dispose();
    //    _RSIndexDataset.Dispose();
    //}

    public DataSet AgriParamDataset
    {
        get { return _AgriParamDataset; }
    }

    public DataSet RSIndexDataset
    {
        get { return _RSIndexDataset; }
    }


    //private DataSet getAgriParamDataSet(string agriParamName, DateTime samplingDate, string districtId)
    //{
    //    string queryString = "";
    //    queryString = "select PointID, " + agriParamName + " from rs.sp_agri_param where sampdate = to_date('" + samplingDate + "') and DistId = '" + districtId + "' orderby PointID";

    //    DataSet ds = OracleDataBase.Query(queryString);
    //    return ds;
    //}

    //private DataSet getRSIndexDataSet( string rsIndexName, DateTime samplingDate, string districtId)
    //{
    //    string queryString = "";
    //    queryString = "select PointID, " + rsIndexName + " from rs.sp_rsindex where sampdate = to_date('" + samplingDate + "') and DistId = '" + districtId + "' orderby PointID";
    //    DataSet ds = OracleDataBase.Query(queryString);
    //    return ds;
    //}



    /// <summary>
    /// 计算相关系数
    /// </summary>
    /// <param name="agriParamName">农学参数名称</param>
    /// <param name="rsIndexName">植被指数名称</param>
    /// <param name="samplingDate">取样时间</param>
    /// <param name="districtId">地区ID</param>
    /// <returns>相关系数R的值</returns>
    public double calculateRelatedCoefficient( DataSet AgriParamDataset, DataSet RSIndexDataset)
    {
        //DataSet AgriParamDataset = this.getAgriParamDataSet(agriParamName, samplingDate, districtId);
        //DataSet RSIndexDataset = this.getRSIndexDataSet(rsIndexName, samplingDate, districtId);

        int AgriPointCount = AgriParamDataset.Tables[0].Rows.Count;
        int RsPointCount = RSIndexDataset.Tables[0].Rows.Count;


        if (AgriPointCount > 0 && AgriPointCount == RsPointCount)
        {
            
            double SumAgriParam = 0.0;
            double SumRsIndex = 0.0;
            double AverageAgriParam = 0.0;
            double AverageRsIndex = 0.0;

            double SumAgriParam_RsIndex = 0.0;
            double QuadSumAgriParam = 0.0;
            double QuadSumRsIndex = 0.0;
            
            //求农学参数与植被指数的平均值
            for (int i = 0; i < AgriPointCount; i++)
            {
                SumAgriParam += Convert.ToDouble(AgriParamDataset.Tables[0].Rows[i][1]);
                SumRsIndex += Convert.ToDouble(RSIndexDataset.Tables[0].Rows[i][1]);
            }
            AverageAgriParam = SumAgriParam / AgriPointCount;
            AverageRsIndex = SumRsIndex / AgriPointCount;
            
            //求计算式的分子和分母
            for (int i = 0; i < AgriPointCount; i++)
            {
                SumAgriParam_RsIndex +=( Convert.ToDouble(AgriParamDataset.Tables[0].Rows[i][1]) - AverageAgriParam) * (Convert.ToDouble(RSIndexDataset.Tables[0].Rows[i][1]) - AverageRsIndex);
                QuadSumAgriParam += ((Convert.ToDouble(AgriParamDataset.Tables[0].Rows[i][1])) - AverageAgriParam) * ((Convert.ToDouble(AgriParamDataset.Tables[0].Rows[i][1])) - AverageAgriParam);
                QuadSumRsIndex += ((Convert.ToDouble(RSIndexDataset.Tables[0].Rows[i][1])) - AverageRsIndex) * ((Convert.ToDouble(RSIndexDataset.Tables[0].Rows[i][1])) - AverageRsIndex);
            }

            double R = SumAgriParam_RsIndex / (Math.Sqrt(QuadSumAgriParam) * Math.Sqrt(QuadSumRsIndex));
            R = Math.Round(R, 4);
            AgriParamDataset.Dispose();
            RSIndexDataset.Dispose();

            return R;
        }


        return 0.0;
 
    }

    /// <summary>
    /// 求解线性模型的参数
    /// </summary>
    /// <param name="agriParamName"></param>
    /// <param name="rsIndexName"></param>
    /// <param name="samplingDate"></param>
    /// <param name="districtId"></param>
    /// <returns></returns>
    public double[] createLinerModel(DataSet AgriParamDataset, DataSet RSIndexDataset)
    {
        //DataSet AgriParamDataset = this.getAgriParamDataSet(agriParamName, samplingDate, districtId);
        //DataSet RSIndexDataset = this.getRSIndexDataSet(rsIndexName, samplingDate, districtId);

        int AgriPointCount = AgriParamDataset.Tables[0].Rows.Count;
        int RsPointCount = RSIndexDataset.Tables[0].Rows.Count;

        
        
        if (AgriPointCount > 0 && AgriPointCount == RsPointCount)
        {
            //根据农学参数和植被指数的dataset计算中间参数
            double SumAgriParam = 0.0;
            double SumRsIndex = 0.0;
            double SumAgriRsIndex = 0.0;
            double SumRsIndexQuad = 0.0;
            for (int i = 0; i < AgriPointCount; i++)
            {
                SumAgriParam += Convert.ToDouble(AgriParamDataset.Tables[0].Rows[i][1]);
                SumRsIndex += Convert.ToDouble(RSIndexDataset.Tables[0].Rows[i][1]);

                SumAgriRsIndex += Convert.ToDouble(AgriParamDataset.Tables[0].Rows[i][1]) * Convert.ToDouble(RSIndexDataset.Tables[0].Rows[i][1]);
                SumRsIndexQuad += Convert.ToDouble(RSIndexDataset.Tables[0].Rows[i][1]) * Convert.ToDouble(RSIndexDataset.Tables[0].Rows[i][1]);
            }

            //计算模型的a b 参数
            double Param_A = (SumAgriParam * SumRsIndexQuad - SumRsIndex * SumAgriRsIndex) / (AgriPointCount * SumRsIndexQuad - SumRsIndex * SumRsIndex);
            double Param_B = (AgriPointCount * SumAgriRsIndex - SumAgriParam * SumRsIndex) / (AgriPointCount * SumRsIndexQuad - SumRsIndex * SumRsIndex);

            //计算模型反演的农学参数数值
            double[] InversionAgriParam = new double[AgriPointCount];
            for (int i = 0; i < AgriPointCount; i++)
            {
                InversionAgriParam[i] = Param_A + Param_B * Convert.ToDouble(RSIndexDataset.Tables[0].Rows[i][1]);
            }

            //计算求解拟合优度的中间参数
            double SumMeasuredValueMinusInversionValue_Quad = 0.0;
            double SumMeasuredValueMinusAverageValue_Quad = 0.0;
            double AverageMeasuredValue = SumAgriParam/AgriPointCount;
            for (int i = 0; i < AgriPointCount; i++)
            {
                SumMeasuredValueMinusInversionValue_Quad += (Convert.ToDouble(AgriParamDataset.Tables[0].Rows[i][1]) - InversionAgriParam[i]) * (Convert.ToDouble(AgriParamDataset.Tables[0].Rows[i][1]) - InversionAgriParam[i]);
                SumMeasuredValueMinusAverageValue_Quad += (Convert.ToDouble(AgriParamDataset.Tables[0].Rows[i][1]) - AverageMeasuredValue) * (Convert.ToDouble(AgriParamDataset.Tables[0].Rows[i][1]) - AverageMeasuredValue);
            }
            //计算拟合优度的值
            double Param_R = 1 - SumMeasuredValueMinusInversionValue_Quad / SumMeasuredValueMinusAverageValue_Quad;
            Param_A = Math.Round(Param_A, 4);
            Param_B = Math.Round(Param_B, 4);
            Param_R = Math.Round(Param_R, 4);

            double[] Param_Model = new double[3] { Param_A, Param_B, Param_R };
            return Param_Model;
        }

        return null;
    }


    /// <summary>
    /// 求解指数模型的参数
    /// </summary>
    /// <param name="agriParamName"></param>
    /// <param name="rsIndexName"></param>
    /// <param name="samplingDate"></param>
    /// <param name="districtId"></param>
    /// <returns></returns>
    public double[] createExpModel(DataSet AgriParamDataset, DataSet RSIndexDataset)
    {
        //DataSet AgriParamDataset = this.getAgriParamDataSet(agriParamName, samplingDate, districtId);
        //DataSet RSIndexDataset = this.getRSIndexDataSet(rsIndexName, samplingDate, districtId);

        int AgriPointCount = AgriParamDataset.Tables[0].Rows.Count;
        int RsPointCount = RSIndexDataset.Tables[0].Rows.Count;


        if (AgriPointCount > 0 && AgriPointCount == RsPointCount)
        {
            double SumAgriParam = 0.0;
            double SumRsIndexExp = 0.0;
            double SumAgriRsIndexExp = 0.0;
            double SumRsIndexExpQuad = 0.0;

            for (int i = 0; i < AgriPointCount; i++)
            {
                SumAgriParam += Convert.ToDouble(AgriParamDataset.Tables[0].Rows[i][1]);
                SumRsIndexExp += Math.Exp(Convert.ToDouble(RSIndexDataset.Tables[0].Rows[i][1]));
                SumAgriRsIndexExp += Math.Exp(Convert.ToDouble(RSIndexDataset.Tables[0].Rows[i][1])) * Convert.ToDouble(AgriParamDataset.Tables[0].Rows[i][1]);
                SumRsIndexExpQuad += Math.Exp(Convert.ToDouble(RSIndexDataset.Tables[0].Rows[i][1]) * 2);
            }
            
            //计算模型的a b 参数
            double Param_A = (SumAgriParam * SumRsIndexExpQuad - SumRsIndexExp * SumAgriRsIndexExp) / (AgriPointCount * SumRsIndexExpQuad - SumRsIndexExp * SumRsIndexExp);
            double Param_B = (AgriPointCount * SumAgriRsIndexExp - SumAgriParam * SumRsIndexExp) / (AgriPointCount * SumRsIndexExpQuad - SumRsIndexExp * SumRsIndexExp);

            //计算模型反演的农学参数数值
            double[] InversionAgriParam = new double[AgriPointCount];
            for (int i = 0; i < AgriPointCount; i++)
            {
                InversionAgriParam[i] = Param_A + Param_B * Math.Exp(Convert.ToDouble(RSIndexDataset.Tables[0].Rows[i][1]));
            }

            //计算求解拟合优度的中间参数
            double SumMeasuredValueMinusInversionValue_Quad = 0.0;
            double SumMeasuredValueMinusAverageValue_Quad = 0.0;
            double AverageMeasuredValue = SumAgriParam / AgriPointCount;
            for (int i = 0; i < AgriPointCount; i++)
            {
                SumMeasuredValueMinusInversionValue_Quad += (Convert.ToDouble(AgriParamDataset.Tables[0].Rows[i][1]) - InversionAgriParam[i]) * (Convert.ToDouble(AgriParamDataset.Tables[0].Rows[i][1]) - InversionAgriParam[i]);
                SumMeasuredValueMinusAverageValue_Quad += (Convert.ToDouble(AgriParamDataset.Tables[0].Rows[i][1]) - AverageMeasuredValue) * (Convert.ToDouble(AgriParamDataset.Tables[0].Rows[i][1]) - AverageMeasuredValue);
            }
            //计算拟合优度的值
            double Param_R = 1 - SumMeasuredValueMinusInversionValue_Quad / SumMeasuredValueMinusAverageValue_Quad;
            Param_A = Math.Round(Param_A, 4);
            Param_B = Math.Round(Param_B, 4);
            Param_R = Math.Round(Param_R, 4);

            double[] Param_Model = new double[3] { Param_A, Param_B, Param_R };
            return Param_Model;
        }
        return null;
    }



    /// <summary>
    /// 求解对数模型的参数
    /// </summary>
    /// <param name="agriParamName"></param>
    /// <param name="rsIndexName"></param>
    /// <param name="samplingDate"></param>
    /// <param name="districtId"></param>
    /// <returns></returns>
    public double[] createLogModel(DataSet AgriParamDataset, DataSet RSIndexDataset)
    {
        //DataSet AgriParamDataset = this.getAgriParamDataSet(agriParamName, samplingDate, districtId);
        //DataSet RSIndexDataset = this.getRSIndexDataSet(rsIndexName, samplingDate, districtId);

        int AgriPointCount = AgriParamDataset.Tables[0].Rows.Count;
        int RsPointCount = RSIndexDataset.Tables[0].Rows.Count;


        if (AgriPointCount > 0 && AgriPointCount == RsPointCount)
        {
            double SumAgriParam = 0.0;
            double SumRsIndexLog = 0.0;
            double SumAgriRsIndexLog = 0.0;
            double SumRsIndexLogQuad = 0.0;

            for (int i = 0; i < AgriPointCount; i++)
            {
                SumAgriParam += Convert.ToDouble(AgriParamDataset.Tables[0].Rows[i][1]);
                SumRsIndexLog += Math.Log(Math.Abs(Convert.ToDouble(RSIndexDataset.Tables[0].Rows[i][1])));
                SumAgriRsIndexLog += Math.Log(Math.Abs(Convert.ToDouble(RSIndexDataset.Tables[0].Rows[i][1]))) * Convert.ToDouble(AgriParamDataset.Tables[0].Rows[i][1]);
                SumRsIndexLogQuad += Math.Log(Math.Abs(Convert.ToDouble(RSIndexDataset.Tables[0].Rows[i][1]))) * Math.Log(Math.Abs(Convert.ToDouble(RSIndexDataset.Tables[0].Rows[i][1])));
            }

            //计算模型的a b 参数
            double Param_A = (SumAgriParam * SumRsIndexLogQuad - SumRsIndexLog * SumAgriRsIndexLog) / (AgriPointCount * SumRsIndexLogQuad - SumRsIndexLog * SumRsIndexLog);
            double Param_B = (AgriPointCount * SumAgriRsIndexLog - SumAgriParam * SumRsIndexLog) / (AgriPointCount * SumRsIndexLogQuad - SumRsIndexLog * SumRsIndexLog);


            //计算模型反演的农学参数数值
            double[] InversionAgriParam = new double[AgriPointCount];
            for (int i = 0; i < AgriPointCount; i++)
            {
                InversionAgriParam[i] = Param_A + Param_B * Math.Log(Math.Abs(Convert.ToDouble(RSIndexDataset.Tables[0].Rows[i][1])));
            }

            //计算求解拟合优度的中间参数
            double SumMeasuredValueMinusInversionValue_Quad = 0.0;
            double SumMeasuredValueMinusAverageValue_Quad = 0.0;
            double AverageMeasuredValue = SumAgriParam / AgriPointCount;
            for (int i = 0; i < AgriPointCount; i++)
            {
                SumMeasuredValueMinusInversionValue_Quad += (Convert.ToDouble(AgriParamDataset.Tables[0].Rows[i][1]) - InversionAgriParam[i]) * (Convert.ToDouble(AgriParamDataset.Tables[0].Rows[i][1]) - InversionAgriParam[i]);
                SumMeasuredValueMinusAverageValue_Quad += (Convert.ToDouble(AgriParamDataset.Tables[0].Rows[i][1]) - AverageMeasuredValue) * (Convert.ToDouble(AgriParamDataset.Tables[0].Rows[i][1]) - AverageMeasuredValue);
            }
            //计算拟合优度的值
            double Param_R = 1 - SumMeasuredValueMinusInversionValue_Quad / SumMeasuredValueMinusAverageValue_Quad;
            Param_A = Math.Round(Param_A, 4);
            Param_B = Math.Round(Param_B, 4);
            Param_R = Math.Round(Param_R, 4);

            double[] Param_Model = new double[3] { Param_A, Param_B, Param_R };
            return Param_Model;
        }
        return null;
    
    
	}
}
